Repository navigation
Pyjoyment.Plugin.SecurityHeaders: csp_nonce, the nonce of the response - #638
Merged
Merged
Conversation
The helper csp_nonce makes a nonce of 128 random bits when it is first used, and the hook adds it to the directives of the scripts and the styles of the policies, and Cache-Control: private to a response without one, as a shared cache would give the nonce to every client. The inline scripts and styles of the tag helpers javascript and stylesheet get it when the application has the helper, and the bundled templates use them, with no style attribute, so that a strict policy does not block them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Docker Smoke Test
|
Contributor
Tests
|
6 of 7 tasks
7 tasks done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #634, its third step, stacked on #636.
Changes
Pyjoyment.Plugin.SecurityHeadershas the helpercsp_nonce: 128 random bits in base64, made when first used and the same for the whole request.'nonce-…'todefault-src,script-src,script-src-elem,style-srcandstyle-src-elem, in the dict policies of bothcspandcsp_report_only, wherever the policy has them. A policy without those directives restricts neither scripts nor styles, so it gets no nonce.Cache-Control: privateunless it already has aCache-Control. A shared cache such as CloudFront would otherwise give the same nonce to every client, so it would no longer be secret. A page without inline code does not use the nonce and stays cacheable.Pyjoyment.Plugin.TagHelpers: the inline forms ofjavascriptandstylesheetadd thenonceattribute when the application has a helpercsp_nonce. That can be the plugin's helper or one of the application's own, for a CSP set by hand. Without one, the output is the same as before, so the tests ported from Mojolicious are unchanged. A script with a URL gets no nonce, as'self'covers it; with'strict-dynamic'the template passesnonce=c.csp_nonce().exception.html.ep,not_found.html.ep,debug.html.ep): their inline<style>and<script>use the tag helpers (# Unlike Mojolicious:), and thestyleattribute of the GitHub icon on the debug page moved to the stylesheet, as a nonce does not cover attributes. A strict policy therefore no longer blocks the error pages or the debug page.Validation
t/Pyjoyment/security_headers_lite_app.py, subtestNonce:styleattributeCache-Control: privateis added, and aCache-Controlset by the action is keptCache-Controlnonceattributet/docs/Pyjoyment_Plugin_SecurityHeaders.py: the examples ofcsp_noncet/Pyjoyment/tag_helper_lite_app.pyandt/docs/Pyjoyment_Plugin_TagHelpers.pyunchanged and passingPYTHONDEVMODE=1 python -m Pyjo.Prove -l -j 4 -r ton asyncio and onPyjo.Reactor.Poll: all tests passsphinx-build -Wpassestrunk check: no issues🤖 Generated with Claude Code